package com.jing.my;

import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ShortcutInfo;
import android.content.pm.ShortcutManager;
import android.net.Uri;
import android.os.Handler;
import android.view.View;
import android.os.Bundle;
import android.view.ViewGroup;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.core.content.ContextCompat;
import androidx.core.content.pm.ShortcutInfoCompat;
import androidx.core.content.pm.ShortcutManagerCompat;
import androidx.core.graphics.drawable.IconCompat;
import androidx.fragment.app.Fragment;
import com.google.android.material.button.MaterialButton;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import com.google.android.material.textfield.TextInputEditText;
//import com.google.gson.Gson;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;
import dev.utils.app.toast.ToastUtils;
import java.util.ArrayList;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.json.JSONObject;
import xyz.doikki.videocontroller.StandardVideoController;
import xyz.doikki.videoplayer.player.VideoView;

public class fragtwo extends Fragment {
	MaterialButton mb1, mb2, mb3;
	Context c = null;
	long time,firstclick=0,secondclick=0;
	Response re;
	String url = "http://xiaobai.klizi.cn/API/other/douyin.php";
	String s, returned, pate = "=";
	Data ddt;
	VideoView vv;
	ArrayList<String> array = new ArrayList<String>();

	//	String url="http://videoringbssdlbig.ali.kugou.com/202207110605/12e21acd8a063d1b5e3d3b487fe99952/f57e807e53b74b8b25fe50d31ed91945.mp4";
	@Override
	public View onCreateView(LayoutInflater arg0, ViewGroup arg1, Bundle arg2) {
		View myview = arg0.inflate(R.layout.two, arg1, false);
		c = getContext();
		mb1 = myview.findViewById(R.id.text);
		mb2 = myview.findViewById(R.id.outlined);
		mb3 = myview.findViewById(R.id.elevated);
		XXPermissions.with(c).permission(Permission.WRITE_EXTERNAL_STORAGE).permission(Permission.READ_EXTERNAL_STORAGE)
				.request((a, b) -> {

					//	ToastUtils.showShort("Success!");

					//ToastUtils.showShort("Failed");
				});
		vv = myview.findViewById(R.id.player);
		StandardVideoController sc = new StandardVideoController(c);
		sc.addDefaultControlComponent("Lan", false);
		vv.setVideoController(sc);
		mb2.setOnLongClickListener((a) -> {
			View vb = View.inflate(c, R.layout.video, null);
			TextInputEditText tt = vb.findViewById(R.id.tst);
			new MaterialAlertDialogBuilder(c).setView(vb).setPositiveButton("Ok", (as, ad) -> {
				//	txt=tt.getText().toString().trim();
			}).setNegativeButton("cancel", (aw, ac) -> {

			}).show();
			return true;
		});
		mb2.setOnClickListener(v -> {
		doubleclick();
		//	view();

		});

		return myview;

	}
void doubleclick()
		{
		if(firstclick==0&&secondclick==0)
				{
				firstclick=System.currentTimeMillis();	
					
					}
					else if(firstclick!=0&&secondclick==0)	
			{
				secondclick=System.currentTimeMillis();
				long cost=(secondclick-firstclick)/1000;
				ToastUtils.showLong("cost"+cost+"s");
				firstclick=0;
				secondclick=0;
				}
			}
	void view()

	{
		new Thread(() -> {
			OkHttpClient oc = new OkHttpClient();
			Request r = new Request.Builder().url(url).build();
			try {
				Response re = oc.newCall(r).execute();
				returned = re.body().string();
				JSONObject jo = new JSONObject(returned);
				array.clear();
				array.add(jo.getString("desc"));
				array.add(jo.getString("author"));
				array.add(jo.getString("cover"));
				array.add(jo.getString("video"));

			} catch (Exception e) {
			}

		}).start();
		if (array.size() != 0) {
			if (pate.equals(array.get(3))) {
				view();
			} else {
				pate = array.get(3);
				vv.release();
				vv.setUrl(pate);
				vv.start();
			}

		}

	}

	@Override
	public void onPause() {
		super.onPause();
		vv.release();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		vv.release();
	}

	@Override
	public void onResume() {
		super.onResume();
		vv.release();
	}

	@Override
	public void onStop() {
		super.onStop();
		vv.release();
	}

	void create() {
		Intent i = new Intent();
		i.setAction("LOCATION_SHORTCUT");
		i.setClass(getContext(), SecondActivity.class);
		ShortcutInfoCompat shortcut = new ShortcutInfoCompat.Builder(getContext(), "first shortcut")
				.setShortLabel("shortcut").setLongLabel("my first shortcut")
				.setIcon(IconCompat.createWithResource(getContext(), R.drawable.twotone_dashboard_black_48))
				.setIntent(i).build();
		ShortcutManagerCompat.pushDynamicShortcut(getContext(), shortcut);

	}

	void pinned() {
		ShortcutManager shom = getContext().getSystemService(ShortcutManager.class);
		if (shom.isRequestPinShortcutSupported()) {
			ShortcutInfo info = new ShortcutInfo.Builder(getContext(), "pinnedshortcut").build();
			Intent it = shom.createShortcutResultIntent(info);
			it.setAction(Intent.ACTION_VIEW);
			PendingIntent pi = PendingIntent.getBroadcast(getContext(), 0, it, PendingIntent.FLAG_UPDATE_CURRENT);
			shom.requestPinShortcut(info, pi.getIntentSender());

		}

	}
}